<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
// load base class if needed
require_once( APPPATH . 'controllers/base/PublicBase.php' );

class penilaian extends ApplicationBase {

    // constructor
    public function __construct() {
        parent::__construct();
        // load model
        $this->load->model('m_public');
        // load library
        $this->load->library("tnotification");
        $this->load->library("pagination");
    }

    // index
    public function index() {
        // set template content
        $this->smarty->assign("template_content", "public/penilaian.html");
        // data session
        $search = $this->tsession->userdata('search_kelas');
        $this->smarty->assign('search', $search);
        // params
        $kelas_nm = !empty($search['kelas_nm']) ? "%" . $search['kelas_nm'] . "%" : "%";
        //--
        $params = array($kelas_nm);
        /* start of pagination --------------------- */
        // pagination
        $config['base_url'] = site_url("public/penilaian/index/");
        $config['total_rows'] = $this->m_public->get_total_kelas_open($params);
        $config['uri_segment'] = 4;
        $config['per_page'] = 20;
        $this->pagination->initialize($config);
        $pagination['data'] = $this->pagination->create_links();
        // pagination attribute
        $start = $this->uri->segment(4, 0) + 1;
        $end = $this->uri->segment(4, 0) + $config['per_page'];
        $end = (($end > $config['total_rows']) ? $config['total_rows'] : $end);
        $pagination['start'] = ($config['total_rows'] == 0) ? 0 : $start;
        $pagination['end'] = $end;
        $pagination['total'] = $config['total_rows'];
        // pagination assign value
        $this->smarty->assign("pagination", $pagination);
        $this->smarty->assign("no", $start);
        /* end of pagination ---------------------- */
        // get data
        $params = array($kelas_nm, ($start - 1), $config['per_page']);
        $result = $this->m_public->get_data_kelas_open($params);
        $this->smarty->assign('rs_id', $result);
        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    // proses search
    public function search_kelas() {
        if ($this->input->post('save') == 'Cari') {
            // params
            $params = array("kelas_nm" => $this->input->post('kelas_nm'));
            // set session
            $this->tsession->set_userdata('search_kelas', $params);
        } else {
            // unset session
            $this->tsession->unset_userdata('search_kelas');
        }
        // redirect
        redirect('public/penilaian');
    }

    // halaman input penilaian dosen
    public function input($kelas_detail_kd = "") {
        // set template content
        $this->smarty->assign("template_content", "public/input.html");
        // data kelas
        $rs_kelas = $this->m_public->get_detail_data_kelas($kelas_detail_kd);
        $this->smarty->assign('rs_kelas', $rs_kelas);
        // data instrumen penilaian
        $rs_instrumen = $this->m_public->get_instrumen_penilaian();
        $this->smarty->assign('rs_instrumen', $rs_instrumen);
        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    // proses input penilaian
    public function input_proses() {
        // cek input
        $this->tnotification->set_rules('nomor_nim', 'Nomor Induk Mahasiswa', 'trim|required|maxlength[20]');
        $this->tnotification->set_rules('kelas_detail_kd', 'Kode Detail Kelas', 'required');
        $this->tnotification->set_rules('tahun', 'Tahun Ajaran', 'required');
        $this->tnotification->set_rules('semester', 'Semester', 'required');
        $this->tnotification->set_rules('dosen_kd', 'Kode Dosen', 'required');
        $this->tnotification->set_rules('penilaian_value[]', 'Penilaian');
        // proses
        if ($this->tnotification->run() !== FALSE) {
            // params
            $params = array(
                $this->input->post('tahun'), $this->input->post('semester'),
                $this->input->post('kelas_detail_kd'), $this->input->post('nomor_nim'),
                $this->input->post('dosen_kd')
            );
            // insert
            if ($this->m_public->insert_info_penilaian($params)) {
                // input penilaian
                $input_kd = $this->m_public->get_last_inserted_id();
                //--
                $this->m_public->delete_data_penilaian($input_kd);
                //--
                $penilaian = $this->input->post('penilaian_value');
                foreach ($penilaian as $key => $value) {
                    $params = array($input_kd, $key, $value);
                    $this->m_public->insert_data_penilaian($params);
                }
                // sukses
                $this->tnotification->delete_last_field();
                $this->tnotification->sent_notification("success", "Data berhasil disimpan");
                // redirect
                redirect('public/penilaian');
            } else {
                // default error
                $this->tnotification->sent_notification("error", "Data gagal disimpan");
            }
        } else {
            // default error
            $this->tnotification->sent_notification("error", "Data gagal disimpan");
        }
        // default redirect
        redirect('public/penilaian/input/' . $this->input->post('kelas_detail_kd'));
    }

}